package com.fywsy.fyweb.strategy;

import com.fywsy.fyweb.entity.SendItem;
import com.fywsy.fyweb.utils.RestTemplateUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.time.LocalDateTime;
import java.time.Duration;

/**
 * 问道-斩神超变
 *
 * 游戏后台实现类
 * 注解gameId对应数据库游戏id
 * 注解GongnengMethod对应数据库功能id
 */
@Component
@GameId(42)
public class WdLingzeGameStrategy extends BaseGameStrategy {
    public static final String LOGIN = "http://43.159.53.8:8521/lingzhe/zhuanyongwangye/sys/user/login";

    public static final String PAY = "http://43.159.53.8:8521/lingzhe/zhuanyongwangye/account/chargeMoney";

    public static final String MAIL = "http://43.159.53.8:8521/lingzhe/zhuanyongwangye/sys/wd/mail/sendMail";

    // 缓存cookie和时间戳
    private static String cachedCookie = null;
    private static LocalDateTime lastLoginTime = null;
    private static final String USERNAME = "admin";
    private static final String PASSWORD = "diyao88";
    private static final long COOKIE_EXPIRE_MINUTES = 30; // 2小时

    /**
     * 登录并获取cookie，自动缓存和续期
     */
    private synchronized String loginAndGetCookie() {
        if (cachedCookie != null && lastLoginTime != null) {
            Duration duration = Duration.between(lastLoginTime, LocalDateTime.now());
            if (duration.toMinutes() < COOKIE_EXPIRE_MINUTES) {
                return cachedCookie;
            }
        }
        // 重新登录
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("username", USERNAME);
        formData.add("password", PASSWORD);
        String cookie = RestTemplateUtil.sendPostFormAndGetCookie(LOGIN, formData);
        cachedCookie = cookie;
        lastLoginTime = LocalDateTime.now();
        return cookie;
    }

    /**
     * 支付接口，自动带cookie
     */
    @GongnengMethod(4201)
    public String pay(SendItem item) {
        // 解析uid，取account
        String[] arr = item.uid != null ? item.uid.split("\\+") : new String[0];
        if (arr.length != 2) return "uid格式错误，应为: 账号+角色名，加号不要漏了";
        String account = arr[0];
        String cookie = loginAndGetCookie();
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("name", account);
        formData.add("money", item.number);
        formData.add("isAddDaili", "0");
        formData.add("isAddRank", "1");
        formData.add("remark", "1");
        String result = RestTemplateUtil.sendPostFormAndGetJsonKeyWithCookie(PAY, formData, "status", cookie);
        return "200".equals(result) ? "发送成功" : "发送失败";
    }

    @GongnengMethod(4202)
    public String mail(SendItem item) {
        // 解析uid
        String[] arr = item.uid != null ? item.uid.split("\\+") : new String[0];
        if (arr.length != 2) return "uid格式错误，应为: 账号+角色名，加号不要漏了";
        String name = arr[1];
        String cookie = loginAndGetCookie();
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("title", "邮件通知");
        formData.add("toName", name);
        formData.add("maps", "");
        formData.add("type", "0");
        formData.add("online", "0");
        formData.add("attachment", "物品-" + item.itemId + "-物品-" + (item.number != null ? item.number : "1"));
        formData.add("msg", "邮件内容");
        String result = RestTemplateUtil.sendPostFormAndGetJsonKeyWithCookie(MAIL, formData, "retmsg", cookie);
        return result;
    }

    @GongnengMethod(4203)
    public String pet(SendItem item) {
        // 解析uid
        String[] arr = item.uid != null ? item.uid.split("\\+") : new String[0];
        if (arr.length != 2) return "uid格式错误，应为: 账号+角色名，加号不要漏了";
        String name = arr[1];
        String cookie = loginAndGetCookie();
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("title", "邮件通知");
        formData.add("toName", name);
        formData.add("maps", "");
        formData.add("type", "0");
        formData.add("online", "0");
        formData.add("attachment", "天神-" + item.itemId + "-天神-" + (item.number != null ? item.number : "1"));
        formData.add("msg", "邮件内容");
        String result = RestTemplateUtil.sendPostFormAndGetJsonKeyWithCookie(MAIL, formData, "retmsg", cookie);
        return result;
    }

    /**
     * 代金券-100-代金券
     * @return
     */
    public String test(String item) {
        String cookie = loginAndGetCookie();
        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
        formData.add("title", "邮件通知");
        formData.add("toName", "肉11");
        formData.add("maps", "");
        formData.add("type", "0");
        formData.add("online", "0");
        formData.add("attachment", item);
        formData.add("msg", "邮件内容");
        String result = RestTemplateUtil.sendPostFormAndGetJsonKeyWithCookie(MAIL, formData, "retmsg", cookie);
        return result;
    }

    public static void main(String[] args) {
        System.out.println(new WdLingzeGameStrategy().test("抽奖次数-100-抽奖次数"));
//        System.out.println(new WdLingzeGameStrategy().test("抽奖次数-100-抽奖次数"));
    }
}

